package com.example.service;

import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;
    /**
     * 登录
     */
    public User login(User user) {

        User dbUser = userMapper.selectByUsername(user.getUsername());
        if (dbUser == null) {
            //没有用户
            throw new CustomException("账号或密码错误");
        }
        //比较密码
        if (!dbUser.getPassword().equals(user.getPassword())) {
            throw new CustomException("账号或密码错误");
        }
        //验证角色
        if (!dbUser.getRole().equals(user.getRole())) {
            throw new CustomException("账号或密码错误");
        }
        return dbUser;
    }
}
